package com.mmy.controller.oa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.oa.RewardType;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.oa.impl.RewardTypeServiceImpl;

/**
 * 类的介绍：激励类型
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class RewardTypeController {
	
	@Autowired
	RewardTypeServiceImpl rewardTypeServiceImpl;

	/**
	 * 函数功能说明： 跳转
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/rewardtype")
	public String ToRewardType(Model model) {
		try {

			List<RewardType> lstRewardType = rewardTypeServiceImpl.findAll();
			model.addAttribute("ltlist", lstRewardType);
			return "OA/reward/rewardtype";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/createrewardtype")
	public String ToCreateRewardType() {
		return "OA/reward/createrewardtype";
	}

	/**
	 * 函数功能说明： 新建激励类型
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/subcreaterewardtype")
	public String CreateRewardType(RewardType rewardType, HttpSession session) throws Exception {
//		获取最大值
		Object oTypeID = rewardTypeServiceImpl.getMaxTypeID();
		/* 判断数据库是否存在菜单id 如果存在在最大值基础上加一 如果不存在把最大值置为一 */
		if (oTypeID == null) {
			rewardType.setTypeID(1);

		} else {
			rewardType.setTypeID((int) oTypeID + 1);
		}
		String strTypeName = rewardType.getTypeName();

		try {

			/* 判断名称是否为空 */
			if (strTypeName == "" || strTypeName == null) {
				return "test";
			} else {
				/* 判断名称是否已存在 */
				if (rewardTypeServiceImpl.findByTypeName(strTypeName).size() > 0) {

					return "test";
				} else {

					rewardType.setCreateDate(new Date());
					TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
					rewardType.setCreator(tSysUser.getUserName());
					rewardTypeServiceImpl.save(rewardType.getTypeID());
					rewardTypeServiceImpl.save(rewardType);
					return "redirect:rewardtype";

				}
			}

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除激励类型
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@PostMapping("/delrewardtype")
	@ResponseBody
	public String DelRewardType(String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstTypeID = new ArrayList<>();

		for (String str : checklist) {
			lstTypeID.add(Integer.parseInt(str));
		}

		rewardTypeServiceImpl.delRewardType(lstTypeID);

		return "OA/reward/rewardtype";
	}

	/**
	 * 函数功能说明： 修改激励类型
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdaterewardtype")
	public String EnterUpdateRewardType(HttpServletRequest request, Model model) {

		int iTypeID = Integer.parseInt(request.getParameter("iTypeID"));

		RewardType rewardType = rewardTypeServiceImpl.findByTypeID(iTypeID);
		model.addAttribute("rewardType", rewardType);
		return "OA/reward/updaterewardtype";

	}

	@RequestMapping(value = "/updaterewardtype")
	public String UpdateRewardType(RewardType rewardType, HttpSession session) throws Exception {

		try {
			/* 定义一个新的实体类对象 */
			RewardType drewardType = rewardTypeServiceImpl.findByTypeID(rewardType.getTypeID());
			drewardType.setMemo(rewardType.getMemo());
			drewardType.setTypeName(rewardType.getTypeName());
			drewardType.setPayment(rewardType.getPayment());
			drewardType.setModifyDate(new Date());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			drewardType.setReviser(tSysUser.getUserName());
			rewardTypeServiceImpl.save(drewardType);
			return "redirect:rewardtype";

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 查询激励类型
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping(value = "/searchrewardtype")
	public String FindRewardType(HttpServletRequest request, Model model) {

		String strTypeName = request.getParameter("TypeName");
		if (strTypeName == "") {
			List<RewardType> lstRewardType = rewardTypeServiceImpl.findAll();
			model.addAttribute("ltlist", lstRewardType);
			request.setAttribute("strTypeName", strTypeName);
			return "OA/reward/rewardtype";
		} else {
			List<RewardType> lstRewardType = rewardTypeServiceImpl.findByTypeName(strTypeName);
			model.addAttribute("ltlist", lstRewardType);
			request.setAttribute("strTypeName", strTypeName);
			return "OA/reward/rewardtype";
		}

	}

}
